import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QLabel, QGridLayout, QHBoxLayout, QTextEdit
from PyQt5.QtGui import QColor, QPalette
from PyQt5.QtCore import Qt

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QLabel
from PyQt5.QtGui import QColor, QPalette
from PyQt5.QtCore import Qt

class CustomTitleDemo(QWidget):
    def __init__(self):
        super().__init__()
        self.init_ui()
        
    def init_ui(self):
        layout = QVBoxLayout()
        
        self.background_label = QLabel('背景颜色')
        self.background_label.setMinimumHeight(80)
        self.background_label.setStyleSheet("background-color: white; border: 1px solid gray;")
        
        self.text_label = QLabel('文本颜色')
        self.text_label.setMinimumHeight(80)
        self.text_label.setStyleSheet("color: black; background-color: white; border: 1px solid gray;")
        
        bg_btn = QPushButton('设置背景颜色')
        text_btn = QPushButton('设置文本颜色')
        
        bg_btn.clicked.connect(lambda: self.select_color('background'))
        text_btn.clicked.connect(lambda: self.select_color('text'))
        
        layout.addWidget(QLabel('背景颜色:'))
        layout.addWidget(self.background_label)
        layout.addWidget(bg_btn)
        layout.addWidget(QLabel('文本颜色:'))
        layout.addWidget(self.text_label)
        layout.addWidget(text_btn)
        
        self.setLayout(layout)
        self.setWindowTitle('自定义标题演示')
        
    def select_color(self, color_type):
        from PyQt5.QtWidgets import QColorDialog
        
        if color_type == 'background':
            title = "选择背景颜色"
            initial = QColor(255, 255, 255)  # 白色
        else:
            title = "选择文本颜色" 
            initial = QColor(0, 0, 0)  # 黑色
            
        color = QColorDialog.getColor(
            initial=initial,
            parent=self,
            title=title
        )
        
        if color.isValid():
            if color_type == 'background':
                self.background_label.setStyleSheet(f"background-color: {color.name()}; border: 1px solid gray;")
            else:
                self.text_label.setStyleSheet(f"color: {color.name()}; background-color: white; border: 1px solid gray;")


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = CustomTitleDemo()
    window.show()
    sys.exit(app.exec_())
